const { contextBridge, ipcRenderer } = require('electron')

// 暴露安全的API给渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
  // 主密码相关
  verifyMasterPassword: (password) => ipcRenderer.invoke('verify-master-password', password),
  setupMasterPassword: (password) => ipcRenderer.invoke('setup-master-password', password),
  
  // 数据操作
  loadData: (masterPassword) => ipcRenderer.invoke('load-data', masterPassword),
  saveData: (data) => ipcRenderer.invoke('save-data', data),
  
  // 系统功能
  copyToClipboard: (text) => ipcRenderer.invoke('copy-to-clipboard', text),
  checkDataFile: () => ipcRenderer.invoke('check-data-file'),
  
  // 应用设置
  loadAppSettings: () => ipcRenderer.invoke('load-app-settings'),
  saveAppSettings: (settings) => ipcRenderer.invoke('save-app-settings', settings),
  showMainWindow: () => ipcRenderer.invoke('show-main-window'),
  hideMainWindow: () => ipcRenderer.invoke('hide-main-window'),
  
  // 菜单事件监听
  onMenuAction: (callback) => {
    ipcRenderer.on('menu-new-account', () => callback('menu-new-account'))
    ipcRenderer.on('menu-refresh', () => callback('menu-refresh'))
    ipcRenderer.on('menu-import', () => callback('menu-import'))
    ipcRenderer.on('menu-export', () => callback('menu-export'))
    ipcRenderer.on('menu-password-generator', () => callback('menu-password-generator'))
    ipcRenderer.on('menu-security-check', () => callback('menu-security-check'))
    ipcRenderer.on('menu-search', () => callback('menu-search'))
    ipcRenderer.on('menu-clear-search', () => callback('menu-clear-search'))
    ipcRenderer.on('menu-copy-username', () => callback('menu-copy-username'))
    ipcRenderer.on('menu-copy-password', () => callback('menu-copy-password'))
    ipcRenderer.on('menu-password-analysis', () => callback('menu-password-analysis'))
    ipcRenderer.on('menu-duplicate-check', () => callback('menu-duplicate-check'))
    ipcRenderer.on('menu-backup', () => callback('menu-backup'))
    ipcRenderer.on('menu-cleanup', () => callback('menu-cleanup'))
    ipcRenderer.on('app-lock', () => callback('app-lock'))
  },
  
  // 移除监听器
  removeMenuListeners: () => {
    ipcRenderer.removeAllListeners('menu-new-account')
    ipcRenderer.removeAllListeners('menu-refresh')
    ipcRenderer.removeAllListeners('menu-import')
    ipcRenderer.removeAllListeners('menu-export')
    ipcRenderer.removeAllListeners('menu-password-generator')
    ipcRenderer.removeAllListeners('menu-security-check')
    ipcRenderer.removeAllListeners('menu-search')
    ipcRenderer.removeAllListeners('menu-clear-search')
    ipcRenderer.removeAllListeners('menu-copy-username')
    ipcRenderer.removeAllListeners('menu-copy-password')
    ipcRenderer.removeAllListeners('menu-password-analysis')
    ipcRenderer.removeAllListeners('menu-duplicate-check')
    ipcRenderer.removeAllListeners('menu-backup')
    ipcRenderer.removeAllListeners('menu-cleanup')
    ipcRenderer.removeAllListeners('app-lock')
  }
})
